package com.cyjt.operation.ui;

import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.cyjt.operation.R;
import com.cyjt.operation.adapter.DynamicNodesAdpter;
import com.cyjt.operation.base.AppContext;
import com.cyjt.operation.base.HandlerMessageCodes;
import com.cyjt.operation.bean.DynamicNodes;
import com.cyjt.operation.bean.RoadsAreas;

/**
 * 动态车位的Activity
 * 
 * @author LTP
 *
 */
public class DynamicParkingLotActivity extends Activity implements OnClickListener, OnItemClickListener {
	/** ActionBar的返回键 */
	private ImageView actionbar_imageView_back_button;
	/** ActionBar的大标题 */
	private TextView actionbar_textView_title;
	/** ActionBar的小标题 */
	private TextView actionbar_textView_small_title;
	/** ActionBar的进度条 */
	private ProgressBar actionbar_progressBar;
	/** 路段下的动态车位列表 */
	private ListView listView_dynamic_parking_lot_list;
	/** ListView的适配器 */
	private DynamicNodesAdpter mDynamicNodesAdpter;
	private List<DynamicNodes> dynamicNodesList;
	private RoadsAreas currentRoadArea;
	/** 从上一个Activity传的小标题 */
	private String smallTitle;
	@SuppressLint("HandlerLeak")
	private Handler handler = new Handler() {
		@SuppressWarnings("unchecked")
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case HandlerMessageCodes.FETCH_DYNAMIC_PARKING_LOTS_SUCCEED:
				dynamicNodesList = (List<DynamicNodes>) msg.obj;
				loadingDynamicParkingLotsList();// 加载路段下的动态车位列表
				break;
			case HandlerMessageCodes.FETCH_DYNAMIC_PARKING_LOTS_FAILED:
				Toast.makeText(DynamicParkingLotActivity.this, "路段下的动态车位列表为空", Toast.LENGTH_SHORT).show();
				break;

			default:
				break;
			}
		}

	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dynamic_parking_lot);
		if (getIntent().getSerializableExtra("roadsAreas") == null) {
			return;
		}
		currentRoadArea = (RoadsAreas) getIntent().getSerializableExtra("roadsAreas");
		smallTitle = getIntent().getStringExtra("smallTitle");
		// 从服务器获取路段下的动态车位信息
		AppContext.getInstance().getVolleyTools().fetchRoadAreasDynamicLots(handler, currentRoadArea);
		initView();
	}

	private void initView() {
		// TODO Auto-generated method stub
		actionbar_imageView_back_button = (ImageView) findViewById(R.id.actionbar_imageView_back_button);
		actionbar_imageView_back_button.setOnClickListener(this);
		actionbar_textView_title = (TextView) findViewById(R.id.actionbar_textView_title);
		actionbar_textView_title.setText("动态交通车位");
		actionbar_textView_small_title = (TextView) findViewById(R.id.actionbar_textView_small_title);
		actionbar_textView_small_title.setText(smallTitle + "," + currentRoadArea.getTitle());
		actionbar_progressBar = (ProgressBar) findViewById(R.id.actionbar_progressBar);
		actionbar_progressBar.setVisibility(View.INVISIBLE);
		listView_dynamic_parking_lot_list = (ListView) findViewById(R.id.listView_dynamic_parking_lot_list);
		listView_dynamic_parking_lot_list.setOnItemClickListener(this);
	}

	/**
	 * 加载路段下的动态车位列表
	 */
	private void loadingDynamicParkingLotsList() {
		// TODO Auto-generated method stub
		mDynamicNodesAdpter = new DynamicNodesAdpter(this, R.layout.list_view_item_for_dynamic_nodes, dynamicNodesList);
		listView_dynamic_parking_lot_list.setAdapter(mDynamicNodesAdpter);
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.actionbar_imageView_back_button:
			onBackPressed();
			break;

		default:
			break;
		}

	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		// TODO Auto-generated method stub
		DynamicNodes currentDynamicNodes = dynamicNodesList.get(position);
		Intent intent = new Intent(this, DynamicParkingLotDeployActivity.class);
		intent.putExtra("dynamicNodes", currentDynamicNodes);
		intent.putExtra("dynamicNodesSize", dynamicNodesList.size());
		startActivity(intent);
	}
}
